﻿using CKS.Core.Framework;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ZR.Model.Enum;
using ZR.Model.Models.ProductionControl;

namespace CKS.PLC.Action.Subscription.OP30
{
    /*
     *  OP30  底板组装 db块是 3051 、 3053
     * BottomPlateRivetingAssemble
     */
    public class OP030SaveDataAction : PLCAction
    {
        public OP030SaveDataAction(DecodeValue _value) : base(_value)
        {
        }
        public override void Execute()
        {
            Dictionary<int, int> replydic = new Dictionary<int, int>();
            replydic.Add(3051,3050);
            replydic.Add(3053, 3052);

            Dictionary<int, int> channeldic = new Dictionary<int, int>();
            channeldic.Add(3051, 1);
            channeldic.Add(3053, 2);
            var channel =channeldic[Value.Position.BlockDb];


            var replydb = replydic[Value.Position.BlockDb];
            var replyPos = Value.GetPosition(replydb, Value.Position.ActionId);
            var result = Value.Value.ToInt();

            var saveconfirm =ReadInt16(replyPos);


            if (result == 1)
            {
                if (saveconfirm == 1)
                    return;
                string batchNo = GetPartBatchNo((int)PartTypeEnum.水嘴大焊环);
                string BottomPlateCode = Value.GetLinkData(1).ToString();
                DcBottomplaterivetingassemble dc = null;

                //dc = db.Queryable<DcBottomplaterivetingassemble>().Where(a => a.BottomPlateCode == BottomPlateCode).First();
                bool add = false;
                if (dc == null)
                {
                    dc = new DcBottomplaterivetingassemble();
                    add = true;
                }

                dc.DCId = "";
                dc.PipeBlueCode = Value.GetLinkData(2).ToString();
                dc.BottomPlateBlueCode = Value.GetLinkData(3).ToString();
                dc.BottomPlateCode = BottomPlateCode;

                dc.LowerODResult1 = Value.GetLinkData(4).ToInt();
                dc.RoundnessResult1 = Value.GetLinkData(5).ToInt();
                dc.UpperODResult1 = Value.GetLinkData(6).ToInt();
                dc.ConcentricityResult1 = Value.GetLinkData(7).ToInt();
                dc.LowerODResult2 = Value.GetLinkData(8).ToInt();
                dc.RoundnessResult2 = Value.GetLinkData(9).ToInt();
                dc.UpperODResult2 = Value.GetLinkData(10).ToInt();
                dc.ConcentricityResult2 = Value.GetLinkData(11).ToInt();

                dc.ThreeDLineScanResult = Value.GetLinkData(12).ToInt();
                dc.CircularHoleDetectionResult = Value.GetLinkData(13).ToInt();
                dc.OvalSlotHoleDetectionResult = Value.GetLinkData(14).ToInt();

                dc.LowerODValue1 = Value.GetLinkData(15).ToDecimal();
                dc.RoundnessVal1 = Value.GetLinkData(16).ToDecimal();
                dc.UpperODValue1 = Value.GetLinkData(17).ToDecimal();
                dc.ConcentricityVal1 = Value.GetLinkData(18).ToDecimal();

                dc.LowerODValue2 = Value.GetLinkData(19).ToDecimal();
                dc.RoundnessVal2 = Value.GetLinkData(20).ToDecimal();
                dc.UpperODValue2 = Value.GetLinkData(21).ToDecimal();
                dc.ConcentricityVal2 = Value.GetLinkData(22).ToDecimal();

                dc.PINHeightMax = Value.GetLinkData(23).ToDecimal();
                dc.PINHeightMin = Value.GetLinkData(24).ToDecimal();
                dc.PINFlatness = Value.GetLinkData(25).ToDecimal();
                dc.SlopeProfile = Value.GetLinkData(26).ToDecimal();
                dc.LeftWallHeight = Value.GetLinkData(27).ToDecimal();
                dc.RightWallHeight = Value.GetLinkData(28).ToDecimal();
                dc.RoundHoleDiaInspect = Value.GetLinkData(29).ToDecimal();
                dc.SlotHoleWidthInspect = Value.GetLinkData(30).ToDecimal();
                dc.TotalResult = Value.GetLinkData(31).ToInt();
                dc.WR1Presence = Value.GetLinkData(32).ToInt();
                dc.WR2Presence = Value.GetLinkData(33).ToInt();
                dc.WR1Height = Value.GetLinkData(34).ToDecimal();
                dc.WR2Height = Value.GetLinkData(35).ToDecimal();

                dc.OD1Result = Value.GetLinkData(36).ToInt();
                dc.OD2Result = Value.GetLinkData(37).ToInt();
                dc.OD1Height = Value.GetLinkData(38).ToDecimal();
                dc.OD2Height = Value.GetLinkData(39).ToDecimal();
                dc.LocationId = Value.GetLinkData(40).ToInt();
                dc.BigSolderRingBlueCode = "";
                dc.AssembleTime = DateTime.Now;

                

                CatchAction(() =>
                {
                    if (add)
                    {
                        db.Insertable(dc).ExecuteCommand();
                        UpdateBasketStock(PartTypeEnum.水嘴大焊环);
                    }
                    else
                    {
                        db.Updateable(dc).ExecuteCommand();
                    }

                } , "OP30保存结果");
                EventBus.AddLog($"OP30保存结果-通道:{channel}-{dc.BottomPlateCode}");
                AckWriteInt16(replyPos, 1);
            }
            else
            {
                
                AckWriteInt16(replyPos, 0);
                EventBus.AddLog($"OP30保存结果-通道:{channel}-已重置");
            }
        }
    }
}
